package com.zsrd.system.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsrd.system.domain.SysDict;
import com.zsrd.system.domain.SysMenu;
import com.zsrd.system.domain.dto.SysDictDto;
import com.zsrd.system.mapper.SysDictMapper;
import com.zsrd.system.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xxx
 * @since 2023-11-23
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public List<Map> selectByType(SysDict sysDict) {
        // 导航sql 查这个表的数据
        List<SysDict> list = sysDictMapper.selectType(sysDict);
        Map<String, List<SysDict>> map1 = list.stream().collect(Collectors.groupingBy(SysDict::getDictType));
        // 输出的
        List<Map> out = new ArrayList<>();
        // 遍历key
//        List<SysDict> dictNames = sysDictMapper.selectName();
        List<SysDict> collect1 = list.stream().filter(distinctByKey(SysDict::getDictType)).collect(Collectors.toList());
        //list转换map
        Map<String, String> types = collect1.stream().collect(Collectors.toMap(SysDict::getDictType, SysDict::getTypeName ,(key1, key2) -> key2));
        for (String s : map1.keySet()) {
            HashMap<Object, Object> o = new HashMap<>();
            String typeName = types.get(s);
            o.put("value",s);
            o.put("label",typeName);
            List<Map> l=new ArrayList();
            List<SysDict> items = map1.get(s);
            for (SysDict item : items) {
                HashMap<Object, Object> map2 = new HashMap<>();
                map2.put("label" ,item.getDictName());
                map2.put("value" ,item.getDictCode());
                map2.put("sort" ,item.getSort());
                l.add(map2);
            }
            l.sort(Comparator.comparing(m->(Long)m.get("sort")));
            o.put("children",l);
            out.add(o);
        }
        return out;
    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }



    @Override
    public IPage<SysDict> selectAll(SysDict sysDict) {
        Page<SysDict> page = new Page<>();
        page.setCurrent(sysDict.getPageNum());//0
        page.setSize(sysDict.getPageSize());//10
        IPage<SysDict> list  =  sysDictMapper.selectAll(page,sysDict);
        return list;
    }

    @Override
    public List<SysDictDto> listAll() {
        return sysDictMapper.list();

    }

    @Override
    public void insert(SysDictDto sysDictDto) {
        sysDictMapper.inserts(sysDictDto);
    }
}
